package edu.cuit.avatar;

import java.io.*;

/**
 * FilterInputStream---->DataInputStream:从输入流中读取字节，并将字节转换为合适的基本数据类型或字符串。
 *      readByte(): byte
 *      readShort(): short
 *      readInt(): int
 *      readLong(): long
 *      readFloat(): float
 *      readDouble(): double
 *      readBoolean(): boolean
 *      readChar(): char
 *
 *      readLine(): String 从输入流中读取一行字符，该方法已经过时
 *      readAllBytes(): byte[]
 *      readUTF(): String 以UTF格式读取一个字符
 *
 *
 * FilterOutputStream---->DataOutputStream:将基本数据类型或字符串数据转换为字节,并且将字节输出到输出流。
 *      writeByte(value: int)
 *      writeBoolean(value: boolean)
 *      writeChar(value: char)
 *      writeShort(value: short)
 *      writeInt(value: int)
 *      writeLong(value: long)
 *      writeFloat(value: float)
 *      writeDouble(value: double)
 *
 *      writeBytes(value: String) 向输出流中写一个字符串中字符的低位字节 (ASCII码组成的字符串)
 *      writeChars(value: String) 向输出流中写一个字符串中的每个字符,每个字符2个字节
 *      writeUFT(value: String) 以UTF格式写一个字符串value
 *
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/12 9:41
 */
public class DataStreamDemo {
    public static void main(String[] args) {
        try(var output = new DataOutputStream(new FileOutputStream("temp.dat"))){
            output.writeUTF("小猪Peppa");
            output.writeInt(5);
            output.writeBoolean(true);
            output.writeDouble(96.5);
            output.writeBytes("Female");
        } catch (IOException e){
            e.printStackTrace();
        }

        //注:应该按照存储的顺序和格式读取文件中的数据
        try(var input = new DataInputStream(new FileInputStream("temp.dat"))){
            System.out.println(input.readUTF());
            System.out.println(input.readInt());
            System.out.println(input.readBoolean());
            System.out.println(input.readDouble());
            System.out.println(new String(input.readAllBytes()));
            System.out.println(input.readUTF()); //如果达到了InputStream的末尾之后还继续从中读取数据,会发生EOFException异常
        } catch (EOFException e){
            System.out.println("文件读取完毕...");
        } catch (IOException e){
            e.printStackTrace();
        }
    }
}
